#include<allegro.h>
#include <stdio.h>
#include <apeg.h>
#include"leo.h"
#include"zombie.h"
#include "bullet.h"
#include "score.h"
#include "background.h"
#include "menu.h"
#include "vie.h"


int Random(int min , int max);
int main()
{
    BITMAP *Buffer;
    //BITMAP* background;
    Background background;
    Leo leo;
    Zombie zombie;
    leo_vie vie1;
    zombie_vie vie2;
    //Zombie2 zombie2;
    Bullet bullet;
    menu menu;
//    Etoile etoile;
    int compteur;
    int compt;
    int comp;
    int a, cpt=0;
    int score=0;


    allegro_init();
    install_timer();
    install_keyboard();
    if (install_keyboard() == -1)
    {
        allegro_message("Error! %s", allegro_error);
        return (-1);
    }
    set_color_depth(24);
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, GFX_W, GFX_H, 0, 0) != 0)
    {
        set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
        allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
        return (1);
    }
    install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL);
    if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL))
    {
        allegro_message("Sound Error: %s", allegro_error);
        return 1;
    }
    load_background(&background);
    load_zombie(&zombie);
    load_leo(&leo);
    load_bullet(&bullet);
    SAMPLE *sample = load_sample("bullet.wav");
    SAMPLE *sample1 = load_sample("song.wav");
//    load_etoile(&etoile);
    compteur = 0;
    compt = 0;
    comp = 0;
    zombie.vie = 100;
    init_leo(&leo);
    init_zombie(&zombie);
    init_vie_l (&vie1);
    init_vie (&vie2);
//    init_etoile(&etoile);
    Buffer = create_bitmap(SCREEN_W, SCREEN_H);
    //    clear_to_color(screen, makecol(0, 0, 0));
    apeg_play_mpg("jeu.ogv", screen, FALSE, NULL);
    load_menu(&menu);
    if((contol_menu(&menu))==1)
    {


        while (!key[KEY_ESC])
        {


            play_sample(sample1 , 255, 128, 1000, FALSE);



            blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
            clear_bitmap(Buffer);
            blit_background(&background, Buffer,&cpt, leo);
            move_leo(&leo,&compteur);
            move_zombie(&zombie,&compt);
            draw_leo(leo,Buffer,compteur);
            draw_zombie(zombie,Buffer,compteur);
//          draw_etoile(etoile,Buffer);
            textprintf_ex(Buffer, font, 10, 10, makecol(255, 100, 200),-1, "Score: %d",score);

            if (key[KEY_SPACE])
            {
                play_sample(sample , 255, 128, 1000, FALSE);

                move_bullet(&bullet,&comp,leo);
                draw_bullet(bullet,Buffer,comp);

            }
            //stop_sample(sample);
            rectfill (Buffer,vie2.xs1,vie2.ys1,vie2.xs2,vie2.ys2,vie2.color);
            if (bullet.xb == zombie.x)
            {
                zombie.vie-=10;
                a++;
                score +=10;
                vie2.xs1++;
//            score_etoile (etoile, leo, score, Buffer);
            }
            if (a == 5 || bullet.xb > SCREEN_W-20)
            {
                zombie.x = Random(1000 , 1500);
                init_bullet(&bullet,leo);
            }

            rectfill (Buffer,vie1.x1,vie1.y1,vie1.x2,vie1.y2,vie1.color);

            if (leo.x == zombie.x)
            {

                leo.x =leo.x -200;
                zombie.x=zombie.x+1000;
                score-=10;
                vie1.x2-=20;
            }
            if (zombie.x+110 < 0)
                zombie.x = Random(1000 , 1500);
            if(leo.x==100 && leo.x<1000)
            {
                leo.y=SCREEN_H-500;
            }
            if (key [KEY_S])
            {
//                sauvegarde (leo ,zombie ,etoile);
 //               chargement(leo ,zombie,etoile);
            }

//vie_leo(&leo ,&zombie,&vie1);
//vie_zombie(leo ,zombie,vie2);
            blit(Buffer , screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);


            if (compteur == 10000)
                compteur = 0;
            if (compt == 10000)
                compt = 0;
            if (comp == 10000)
                comp = 0;

        }
    }
    destroy_background(&background);
    destroy_leo(&leo);
    destroy_bullet(&bullet);
    destroy_zombie(&zombie);
    destroy_menu(&menu);
//destroy_etoile(&etoile);
    destroy_bitmap(Buffer);
    clear_bitmap(Buffer);
    return (0);
}
END_OF_MAIN();

int Random(int min , int max)
{
    return (rand()% (max-min))+min;
}



